import 'package:flutter/cupertino.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:mall/page/common/common_webview.dart';
import 'package:mall/page/home/home_page.dart';
import 'package:mall/utils/log_utils.dart';
import 'package:mall/utils/navigator_util.dart';
import 'package:mall/utils/sp_utils.dart';

///代码清单
class FirstGuildPage extends StatefulWidget {
  @override
  _FirstGuildPageState createState() => _FirstGuildPageState();
}

class _FirstGuildPageState extends State<FirstGuildPage> {
  int _currentIndex = 0;
  TapGestureRecognizer _registProtocolRecognizer = TapGestureRecognizer();
  TapGestureRecognizer _privacyProtocolRecognizer = TapGestureRecognizer();

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    return Scaffold(
      backgroundColor: Colors.white,
      body: Stack(
        children: [
          //第一层的图片
          buildBackground(width, height),
          //第二层的点指示
          buildIndfot(),
          //第三层的去首页
          buildGoHome(),
          buildProtcol(),
        ],
      ),
    );
  }

  Positioned buildGoHome() {
    return Positioned(
      left: 0,
      right: 0,
      bottom: 60,
      height: 44,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          //
          AnimatedContainer(
            duration: Duration(milliseconds: 800),
            height: _currentIndex == 0 ? 44 : 0,
            width: _currentIndex == 0 ? 180 : 0,
            child: ElevatedButton(
              child: Text("去首页"),
              onPressed: () {
                LogUtils.e("点击了去首页面");
                SPUtil.save("flutter_ho_isFirst", true);
                NavigatorUtils.goMallMainPage(context);
              },
            ),
          ),
        ],
      ),
    );
  }

  Positioned buildIndfot() {
    return Positioned(
      left: 0,
      right: 0,
      bottom: 60,
      height: 44,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          buidlIndefot(_currentIndex == 0),
          buidlIndefot(_currentIndex == 1),
          buidlIndefot(_currentIndex == 2),
          buidlIndefot(_currentIndex == 3),
        ],
      ),
    );
  }

  Widget buidlIndefot(bool isSelect) {
    return AnimatedContainer(
      height: 12,
      width: isSelect ? 40 : 12,
      margin: EdgeInsets.only(left: 16),
      duration: Duration(milliseconds: 800),
      decoration: BoxDecoration(
        color: Colors.deepOrangeAccent,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
    );
  }

  Positioned buildBackground(double width, double height) {
    return Positioned.fill(
      child: PageView(
        onPageChanged: (value) {
          setState(() {
            _currentIndex = value;
          });
        },
        children: [
          Image.asset(
            "images/spo1.png",
            width: width,
            height: height,
            fit: BoxFit.fill,
          ),
        ],
      ),
    );
  }

  //查看用户协议
  void openUserProtocol(BuildContext context) {
    NavigatorUtils.pushPage(
      context: context,
      targPage: CommonWebViewPage(
        pageTitle: "用户协议",
        htmlUrl: "http://52talkclub.com/fwxy.html",
      ),
    );
  }

  //查看隐私协议
  void openPrivateProtocol(BuildContext context) {
    NavigatorUtils.pushPage(
      context: context,
      targPage: CommonWebViewPage(
        pageTitle: "隐私协议",
        htmlUrl: "http://52talkclub.com/ysxy.html",
      ),
    );
  }

  Positioned buildProtcol() {
    return Positioned(
      left: 0,
      right: 0,
      bottom: 20,
      height: 44,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          RichText(
            text: TextSpan(children: [
              TextSpan(
                text: "《用户协议》",
                style: TextStyle(color: Colors.blue),
                //点击事件
                recognizer: _registProtocolRecognizer
                  ..onTap = () {
                    //打开用户协议
                    openUserProtocol(context);
                  },
              ),
              TextSpan(
                text: "|",
                style: TextStyle(color: Colors.grey[600]),
              ),
              TextSpan(
                text: "《隐私协议》",
                style: TextStyle(color: Colors.blue),
                //点击事件
                recognizer: _privacyProtocolRecognizer
                  ..onTap = () {
                    //打开隐私协议
                    openPrivateProtocol(context);
                  },
              ),
            ]),
          )
        ],
      ),
    );
  }
}
